package me.siphone.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallbackProxy;
import java.util.ArrayList;
import java.util.List;
import me.siphone.adapter.StationAdapter;
import me.siphone.http.api.QueryStationApi;
import me.siphone.http.model.HttpData;
import me.siphone.model.Measurement;
import me.siphone.model.Station;
import org.linphone.R;

public class StationListFragment extends BaseFragment {

    private RecyclerView stationListView;

    @Nullable
    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater,
            @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_station_list, container, false);

        stationListView = view.findViewById(R.id.station_list);
        stationListView.setLayoutManager(
                new LinearLayoutManager(getContext(), RecyclerView.VERTICAL, false));

        if (getArguments() != null) {
            List<Station> stationList = getArguments().getParcelableArrayList("station_list");
            if (stationList != null) {
                stationListView.setAdapter(new StationAdapter(stationList));
                return view;
            }
        }

        queryStationList();

        return view;
    }

    public void update(List<Station> updateData) {
        if (stationListView.getAdapter() instanceof StationAdapter) {
            ((StationAdapter) stationListView.getAdapter()).update(updateData);
        }
    }

    private void queryStationList() {
        EasyHttp.get(this)
                .api(new QueryStationApi())
                .request(
                        new HttpCallbackProxy<HttpData<List<QueryStationApi.Station>>>(this) {
                            @Override
                            public void onHttpSuccess(
                                    @NonNull HttpData<List<QueryStationApi.Station>> result) {
                                super.onHttpSuccess(result);
                                List<Station> stationList = new ArrayList<>();
                                for (QueryStationApi.Station station : result.getRows()) {
                                    List<QueryStationApi.Station.MonitorData> monitorDataList =
                                            station.getMonitorDataList();
                                    List<Measurement> measurementList = new ArrayList<>();
                                    for (QueryStationApi.Station.MonitorData monitorData :
                                            monitorDataList) {
                                        measurementList.add(
                                                new Measurement(
                                                        monitorData.getMeasurementStationType(),
                                                        monitorData.getSynchronizeTimeStr(),
                                                        monitorData.getLatestLevel(),
                                                        monitorData.getUnit(),
                                                        monitorData.getStatusFlag()));
                                    }
                                    Station target =
                                            new Station(
                                                    station.getMeasurementStationNo(),
                                                    station.getStationName(),
                                                    measurementList);
                                    stationList.add(target);
                                }

                                stationListView.setAdapter(new StationAdapter(stationList));
                            }
                        });
    }
}
